package com.ktjiaoyu.config;

import com.ktjiaoyu.cond.ConditionOnPropertyEnable;
import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @return 13141
 * 2025/6/27
 * 09:33
 */
// 标注配置类
@Configuration
// 指定读取外部配置文件
@PropertySource(value = "classpath:database.properties", encoding = "UTF-8")
//@Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
public class MyBatisConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${pool.maxTotal}")
    private Integer maxTotal;
    @Value("${pool.initialSize}")
    private Integer initialSize;
    @Value("${pool.minIdle}")
    private Integer minIdle;
    @Value("${pool.maxIdle}")
    private Integer maxIdle;
    @Value("${pool.maxWaitMillis}")
    private Integer maxWaitMillis;
    @Value("${pool.removeAbandonedOnMaintenance}")
    private boolean removeAbandonedOnMaintenance;
    @Value("${pool.removeAbandonedOnBorrow}")
    private boolean removeAbandonedOnBorrow;
    @Value("${pool.removeAbandonedTimeout}")
    private Integer removeAbandonedTimeout;


//    //     创建 SqlSessionFactoryBean, 引用 testDataSource数据源
//    @Bean
//    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
//        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
//        factoryBean.setDataSource(devDataSource()); // 引用 devDataSource 数据源
//
//        // 设置 MyBatis 的 xml 配置文件路径
//        factoryBean.setConfigLocation(new PathMatchingResourcePatternResolver()
//                .getResource("classpath:mybatis-config.xml"));
//
//        // 设置映射文件路径
//        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
//                .getResource("classpath:mapper/**/*.xml"));
//
//        return factoryBean;
//    }

    // 创建 devDataSource 数据源
    @Bean
    @ConditionOnPropertyEnable(enableProperty = "dev.enable")
    public BasicDataSource devDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaxTotal(maxTotal);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxIdle(maxIdle);
        dataSource.setRemoveAbandonedOnMaintenance(removeAbandonedOnMaintenance);
        dataSource.setRemoveAbandonedOnBorrow(removeAbandonedOnBorrow);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);
        return dataSource;
    }
    // 创建 testDataSource 数据源
    @Bean
    @ConditionOnPropertyEnable(enableProperty = "test.enable")
    public BasicDataSource testDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); // MySQL JDBC 驱动
        dataSource.setUrl("jdbc:mysql://localhost:3306/test_db"); // 数据库 URL
        dataSource.setUsername("root"); // 数据库用户名
        dataSource.setPassword("password"); // 数据库密码
        dataSource.setInitialSize(5); // 初始连接池大小
        dataSource.setMaxTotal(10); // 最大连接数
        dataSource.setMaxIdle(5); // 最大空闲连接数
        dataSource.setMinIdle(2); // 最小空闲连接数
        return dataSource;
    }
}
